<?php

/**
 * 登录
 */

namespace app\api\controller;

use app\common\controller\ApiController;
use app\common\lib\RedisKey;
use app\common\lib\RespCode;
use custom\Redis;
use custom\Token;
use Login\Enter;
use Login\WangYiSms3;
use think\Exception;

class Login extends ApiController {

    protected $notNeedToken = ['login', 'sendlogincode','loginout'];

    protected $notNeedPhone = ['login', 'sendlogincode'];

    protected $resubmitLock = ['sendoldornewMobile'];

    /**
     * 用户登录
     * @return false|string
     */
    public function Login() {
        try {
            $params = $this->checkParams('type');
            if ($params === false) {
                return $this->resErr();
            }
            //根据type判断传参
            Enter::checkParams($params);
            $result = Enter::checkLogin($params, $this->headers);
            if ($result){
                return $this->resData('success', $result);
            }else{
                return $this->resErr();
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 登录验证码
     * @return false|string
     */
    public function sendLoginCode() {
        try {
            $params = $this->checkParams('phone,type');
            if ($params === false) {
                return $this->resErr();
            }
            if (empty($params['phone'])) throw new Exception('请输入手机号', RespCode::PARAMS_ERR);

            if (!preg_match('/^1[3-9]\d{9}$/', $params['phone'])) {
                throw new Exception('请输入正确的手机号', RespCode::PARAMS_ERR);
            }
            $re = WangYiSms3::sendAuthCode(trim($params['phone']), rand(1111,9999), 5, RedisKey::AUTH_CODE_LOCK . $params['type'], RedisKey::VERIFICATION_CODE . $params['type']);
            if ($re) {
                return $this->resData('发送成功', []);
            } else {
                throw new Exception('发送失败', RespCode::PARAMS_ERR);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 1：原手机    2：新手机
     * 原手机/新手机验证码
     * @return false|string
     */
    public function sendOldOrNewMobile() {
        try {
            $params = $this->checkParams('type');
            if ($params === false) {
                return $this->resErr();
            }
            if ($params['type'] == 1) {
                $re = WangYiSms3::sendAuthCode($this->uInfo['phone'], rand(1111,9999), 5, RedisKey::OLD_MOBILE_CODE_LOCK, RedisKey::OLD_MOBILE_CODE);
            }
            if ($params['type'] == 2) {
                if (empty($params['phone'])) throw new Exception('请输入手机号', RespCode::PARAMS_ERR);
                if (!preg_match('/^1[3-9]\d{9}$/', $params['phone'])) {
                    throw new Exception('请输入正确的手机号', RespCode::PARAMS_ERR);
                }
                if ($params['phone'] == $this->uInfo['phone']) throw new Exception('请输入新的手机号', RespCode::PARAMS_ERR);
                $re = WangYiSms3::sendAuthCode($params['phone'], rand(1111,9999), 5, RedisKey::NEW_MOBILE_CODE_LOCK, RedisKey::NEW_MOBILE_CODE);
            }
            if ($re) {
                return $this->resData('发送成功', []);
            } else {
                throw new Exception('发送失败', RespCode::PARAMS_ERR);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 退出登录
     * @return false|string
     */
    public function loginOut() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $token = new Token();
            $token->delUserToken($this->headers['token'], $token::API_TOKEN);
            return $this->resData('操作成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}
